﻿@model List<XukaShop.Models.Entities.ProductSize>


<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button aria-hidden="true" data-dismiss="modal" class="close" type="button">&times;</button>
            <h4 class="modal-title">Số lượng sản phẩm</h4>
        </div>
        <div class="modal-body">
            <form class="form-horizontal form-bordered" id="frmProductSize">
                @foreach (var productSize in Model)
                {
                    <div class="form-group">
                        <input type="text" class="form-control" id="@productSize.Id" name="@productSize.Id" style="display: none" />
                        <div class="col-sm-5">
                            <label class="col-sm-3 control-label">Size</label>
                            <div class="col-sm-8">
                                <div class="input-group">
                                    <input type="text" id="txtProductSize_@productSize.Id" class="form-control" value="@productSize.Size.SizeName" readonly="true"/>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-5">
                            <label class="col-sm-4 control-label">Số lượng</label>
                            <div class="col-sm-8">
                                <div class="input-group">
                                    <input type="text" id="txtProductQuantity_@productSize.Id" class="form-control" value="@productSize.Quantity"/>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-2">
                            <button class="btn btn-primary mr5" type="button" id="btnSaveProductSize" onclick="saveProductSize(@productSize.Id)">Lưu</button>
                        </div>
                    </div>
                }
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    function saveProductSize(productSizeId) {
        var idOfQuantity = "txtProductQuantity_" + productSizeId;
        var idOfSizeName = "txtProductSize_" + productSizeId;
        var quantityValue = $('#' + idOfQuantity).val();
        var sizeNamevalue = $('#' + idOfSizeName).val();
        if (quantityValue != "") {
            jQuery('#' + idOfQuantity).closest('.form-group').removeClass('has-error');
            $.ajax({
                url: '@Url.Action("SaveProductSize", "Product")',
                type: 'POST',
                data: { productSizeId: productSizeId, productSizeQuantity: quantityValue },
                success: function (data) {
                    if (data == "@XukaShop.Common.CommonConst.SUCCESSFUL") {
                        showMessageSaveSizeSuccess(sizeNamevalue);
                    } else {
                        showMessageSaveSizeError(sizeNamevalue);
                    }
                },
                error: function () {
                    showMessageSaveGeneralInfoError(sizeNamevalue);
                }
            });
            }
            else {
                jQuery('#' + idOfQuantity).closest('.form-group').removeClass('has-success').addClass('has-error');
                showMessageInputSizeError(sizeNamevalue);
            }
        }


        function showMessageSaveSizeError(sizeName) {
            jQuery.gritter.add({
                title: 'Thất bại!',
                text: 'Xin hãy nhập số lượng cho size ' + sizeName + ' thất bại!',
                class_name: 'growl-danger',
                sticky: false,
                time: '1000'
            });
            return false;
        }

        function showMessageSaveSizeSuccess(sizeName) {
            jQuery.gritter.add({
                title: 'Thành công!',
                text: 'Bạn đã nhập số lượng cho size ' + sizeName + ' thành công!',
                class_name: 'growl-success',
                sticky: false,
                time: '1000'
            });
            return false;
        }

        function showMessageInputSizeError(sizeName) {
            jQuery.gritter.add({
                title: 'Thất bại!',
                text: 'Xin hãy nhập số lượng cho size ' + sizeName,
                class_name: 'growl-danger',
                sticky: false,
                time: '1000'
            });
            return false;
        }

</script>
